package com.zhanglifan.action;

import com.zhanglifan.domain.ContractProduct;
import com.zhanglifan.service.ContractProductService;
import com.zhanglifan.util.DownloadUtil;
import com.zhanglifan.util.UtilFuns;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.ServletActionContext;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * Information
 * Author: ZhangLifan
 * Time: 2017/10/7 11:28
 * Description: OutProductAction
 */
public class OutProductAction extends BaseAction {

    private String inputDate;

    private ContractProductService contractProductService;

    public void setContractProductService(ContractProductService contractProductService) {
        this.contractProductService = contractProductService;
    }

    public void setInputDate(String inputDate) {
        this.inputDate = inputDate;
    }

    public String toedit(){
        return "toEditSuccess";
    }

//    public String print() throws IOException, ParseException {
//
//        List<ContractProduct> contractProducts = contractProductService.find("from ContractProduct where to_char(contract.shipTime,'yyyy-MM') = ?", ContractProduct.class, new String[]{inputDate});
//
//        int rowIndex = 0;
//        String fileName = inputDate.replace("-","年") + "月出货表";
//        Workbook workbook = new HSSFWorkbook();
//        Sheet sheet = workbook.createSheet("first");
//        Row bigTitle = sheet.createRow(rowIndex++);
//        Cell cell = bigTitle.createCell(1);
//        cell.setCellValue(fileName);
//        cell.setCellStyle(bigTitle(workbook));
//        bigTitle.setHeightInPoints(36);
//        sheet.addMergedRegion(new CellRangeAddress((short)bigTitle.getRowNum(),(short)bigTitle.getRowNum(),(short)cell.getColumnIndex(),(short)(cell.getColumnIndex()+7)));
//
//        Row smallTitle = sheet.createRow(rowIndex++);
//
//        String[] strs = {"客户","订单号","货号","数量","工厂","工厂交期","船期","贸易条款"};
//
//        for (int i=0; i<strs.length;i++) {
//            Cell c = smallTitle.createCell(i + 1);
//            c.setCellStyle(title(workbook));
//            c.setCellValue(strs[i]);
//            if (i%2==1){
//                sheet.setColumnWidth(i+1,24*256);
//            }
//        }
//
//        CellStyle textStyle = text(workbook);
//
//        for (ContractProduct contractProduct : contractProducts) {
//            Row row = sheet.createRow(rowIndex++);
//            row.setHeightInPoints(26);
//
//            Cell cell1 = row.createCell(1);
//            cell1.setCellValue(contractProduct.getContract().getCustomName());
//            cell1.setCellStyle(textStyle);
//
//            Cell cell2 = row.createCell(2);
//            cell2.setCellValue(contractProduct.getOrderNo());
//            cell2.setCellStyle(textStyle);
//
//            Cell cell3 = row.createCell(3);
//            cell3.setCellValue(contractProduct.getProductNo());
//            cell3.setCellStyle(textStyle);
//
//            Cell cell4 = row.createCell(4);
//            cell4.setCellValue(contractProduct.getCnumber());
//            cell4.setCellStyle(textStyle);
//
//            Cell cell5 = row.createCell(5);
//            cell5.setCellValue(contractProduct.getFactoryName());
//            cell5.setCellStyle(textStyle);
//
//            Cell cell6 = row.createCell(6);
//            cell6.setCellValue(UtilFuns.dateTimeFormat(contractProduct.getContract().getDeliveryPeriod()));
//            cell6.setCellStyle(textStyle);
//
//            Cell cell7 = row.createCell(7);
//            cell7.setCellValue(UtilFuns.dateTimeFormat(contractProduct.getContract().getShipTime()));
//            cell7.setCellStyle(textStyle);
//
//            Cell cell8 = row.createCell(8);
//            cell8.setCellValue(contractProduct.getContract().getTradeTerms());
//            cell8.setCellStyle(textStyle);
//        }
//
//
//        ByteArrayOutputStream os = new ByteArrayOutputStream();
//        workbook.write(os);
//        DownloadUtil downloadUtil = new DownloadUtil();
//        downloadUtil.download(os,ServletActionContext.getResponse(),fileName+".xls");
//        workbook.close();
//        os.close();
//        return NONE;
//    }


    public String print() throws IOException, ParseException {

        List<ContractProduct> contractProducts = contractProductService.find("from ContractProduct where to_char(contract.shipTime,'yyyy-MM') = ?", ContractProduct.class, new String[]{inputDate});
        String fileName = inputDate.replace("-","年") + "月出货表";

        String path = ServletActionContext.getServletContext().getRealPath("/make/xlsprint/tOUTPRODUCT.xlsx");
        FileInputStream fileInputStream = new FileInputStream(new File(path));
        Workbook workbook = new XSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheetAt(0);
        int rowIndex = 0;

        Row titleRow = sheet.getRow(rowIndex++);
        titleRow.getCell(1).setCellValue(fileName);

        rowIndex++;

        float heightInPoints = sheet.getRow(rowIndex).getHeightInPoints();

        CellStyle[] cs = new CellStyle[8];
        for (int i = 0 ; i<cs.length; i++) {
            cs[i] = sheet.getRow(rowIndex).getCell(i+1).getCellStyle();
        }

        for (ContractProduct contractProduct: contractProducts) {
            Row row = sheet.createRow(rowIndex++);
            row.setHeightInPoints(heightInPoints);

            Cell cell1 = row.createCell(1);
            cell1.setCellValue(contractProduct.getContract().getCustomName());
            cell1.setCellStyle(cs[0]);

            Cell cell2 = row.createCell(2);
            cell2.setCellValue(contractProduct.getOrderNo());
            cell2.setCellStyle(cs[1]);

            Cell cell3 = row.createCell(3);
            cell3.setCellValue(contractProduct.getProductNo());
            cell3.setCellStyle(cs[2]);

            Cell cell4 = row.createCell(4);
            cell4.setCellValue(contractProduct.getCnumber());
            cell4.setCellStyle(cs[3]);

            Cell cell5 = row.createCell(5);
            cell5.setCellValue(contractProduct.getFactoryName());
            cell5.setCellStyle(cs[4]);

            Cell cell6 = row.createCell(6);
            cell6.setCellValue(UtilFuns.dateTimeFormat(contractProduct.getContract().getDeliveryPeriod()));
            cell6.setCellStyle(cs[5]);

            Cell cell7 = row.createCell(7);
            cell7.setCellValue(UtilFuns.dateTimeFormat(contractProduct.getContract().getShipTime()));
            cell7.setCellStyle(cs[6]);

            Cell cell8 = row.createCell(8);
            cell8.setCellValue(contractProduct.getContract().getTradeTerms());
            cell8.setCellStyle(cs[7]);

        }

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        workbook.write(os);
        DownloadUtil downloadUtil = new DownloadUtil();
        downloadUtil.download(os, ServletActionContext.getResponse(),fileName+".xlsx");
        workbook.close();
        os.close();
        return NONE;
    }


    //大标题的样式
    public CellStyle bigTitle(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)16);
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);					//字体加粗

        style.setFont(font);

        style.setAlignment(CellStyle.ALIGN_CENTER);					//横向居中
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);		//纵向居中

        return style;
    }
    //小标题的样式
    public CellStyle title(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short)12);

        style.setFont(font);

        style.setAlignment(CellStyle.ALIGN_CENTER);					//横向居中
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);		//纵向居中

        style.setBorderTop(CellStyle.BORDER_THIN);					//上细线
        style.setBorderBottom(CellStyle.BORDER_THIN);				//下细线
        style.setBorderLeft(CellStyle.BORDER_THIN);					//左细线
        style.setBorderRight(CellStyle.BORDER_THIN);				//右细线

        return style;
    }

    //文字样式
    public CellStyle text(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short)10);

        style.setFont(font);

        style.setAlignment(CellStyle.ALIGN_LEFT);					//横向居左
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);		//纵向居中

        style.setBorderTop(CellStyle.BORDER_THIN);					//上细线
        style.setBorderBottom(CellStyle.BORDER_THIN);				//下细线
        style.setBorderLeft(CellStyle.BORDER_THIN);					//左细线
        style.setBorderRight(CellStyle.BORDER_THIN);				//右细线

        return style;
    }
}
